package com.chance.cc.crawler.prod.command.job.domain.vm.autohome.adhoc;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.vm.autohome.AutoHomeCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

/**
 * @author lt
 * @version 1.0
 * @date 2021-04-18 18:21:19
 * @email okprog@sina.com
 */
public class AutoHomeArticleSimpleCrawlerSchedulerJob extends AutoHomeCommonCrawlerSchedulerJob {
    private static final String crawler_level = "simple";


    public static void main(String[] args) {

        publishAutoHomeCrawlerScheduleJobInfo();
    }

    /**
     * 发布任务信息
     * @return
     */
    public static CrawlerJob publishAutoHomeCrawlerScheduleJobInfo(){

        CrawlerJob autoHomeScheduler = autoHomeScheduler();

        //发布定时任务
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(autoHomeScheduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("汽车之家新闻临时采集任务");
        crawlerScheduleJob.setCrawlerKey(autoHomeScheduler.generateCrawlerKey());
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布任务：" + httpPage.getRawText());
        return autoHomeScheduler;
    }

    public static CrawlerJob autoHomeScheduler(){

        //文章采集
        CrawlerRequestRecord articleCrawler = doArticleCrawler();
        String site = articleCrawler.tagsCreator().bizTags().getCategoryTag().getKVTagStrVal("site");

        CrawlerJob crawlerJob = CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-", site, crawler_level, CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-", "crawler", domain, site, crawler_level, "queue")))
                .kafkaResultPipeline(null, kafkaTopciForTraceJob, null)
//                .fileResultPipeline("","/data/chance_crawler_runner/logs/node/autohome_article.log",false)
                .crawlerJobThreadNumber(10)
                .requestRecord(articleCrawler)
                .build();
        //添加评论去重信息
        CrawlerRecord filterCrawlerRecord = new CrawlerRecord();
        filterCrawlerRecord.setFilter(CrawlerEnum.CrawlerRecordFilter.dateRange);
        filterCrawlerRecord.addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domain,"comment")));
        filterCrawlerRecord.addFilterInfo(FilterUtils.dateRangeFilterInfo(24 * 3,null));
        articleCrawler.tagsCreator().bizTags().addCustomKV("comment_record_filter_info", JSON.toJSONString(filterCrawlerRecord));
        crawlerJob.getScheduleTags().getCategoryTag().addLabelTag(CrawlerEnum.CrawlerDataType.comment.enumVal());
        return crawlerJob;
    }

    /**
     * 初始record
     * @return
     */
    public static CrawlerRequestRecord doArticleCrawler(){
        String site = "article";

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .domain(domain)
                .recordKey("https://www.autohome.com.cn/channel2/union/list.html")
                .httpUrl("https://www.autohome.com.cn/channel2/union/list.html")
                .releaseTime(System.currentTimeMillis())
                .httpConfig(HttpConfig.me(domain))
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(domain))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*9,null))
                .proxy(proxy)
                .build();

        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSite(site);
        requestRecord.tagsCreator().bizTags().addSiteBiz("news");
        return requestRecord;
    }
}
